import Base from 'models/base';

import SystemUtil from 'utils/systemUtil';

export default class GoodsModelForm extends Base {
  constructor(goodsModel) {
    super();

    if (goodsModel === undefined) {
      this.initData();
    } else {
      this.setData(goodsModel);
    }
  }

  initData() {
    this.goodsModelName = '';
    this.goodsTotal = undefined;
    this.goodsModelCost = undefined;
    this.goodsModelSalePrice = undefined;
    this.goodsModelChannelPrice = undefined;
    this.goodsModelSourcePrice = undefined;
    this.status = 1;
  }

  setData(goodsModel) {
    let fields = ['goodsModelName', 'goodsTotal', 'goodsModelCost', 'goodsModelChannelPrice', 'goodsModelSalePrice', 'status'];
    this.initObject(goodsModel, fields);

    this.id = goodsModel.goodsModelId;
    this.goodsDefId = goodsModel.goodsDefId;

    let goodsModelSourcePrice = goodsModel.goodsModelSourcePrice;
    if (SystemUtil.isEmpty(goodsModelSourcePrice)) {
      goodsModelSourcePrice = undefined;
    }
    this.goodsModelSourcePrice = goodsModelSourcePrice;
  }

  checkValid() {
    let fields = ['goodsModelName', 'goodsTotal', 'goodsModelCost', 'goodsModelChannelPrice', 'goodsModelSalePrice', 'goodsModelSourcePrice'];
    let code  = this.checkRequired(fields);
    if (code !== '') {
      return code;
    }

    if (this.goodsModelSourcePrice < this.goodsModelSalePrice) {
      return 'sourcePrice<salePrice';
    }
    return '';
  }

  getObject(goodsDefId) {
    let postData = {};
    let fields = ['goodsModelName', 'goodsTotal', 'goodsModelCost', 'goodsModelChannelPrice', 'goodsModelSalePrice', 'goodsModelSourcePrice',
      'status'
    ];
    this.setPostData(postData, fields, true);

    let id = this.id;
    if (id !== undefined) {
      postData.goodsModelId = id;
    }

    if (goodsDefId !== undefined) {
      postData.goodsDefId = goodsDefId;
    } else {
      postData.goodsDefId = this.goodsDefId;
    }

    return postData;
  }
}
